package business;
import java.util.*;

public class Theatre 
{
	//member variables
	private EventBook EventCatolog;
	private HallBook HallCatolog;
	private CustomerBook CustomerCatolog;
	private BookingBook BookingCatolog;
	private static boolean ThreadLock;

	//constructor
	public Theatre()
	{
		EventCatolog = EventBook.GetInstance(); //Singleton Class.. Also loads from Persistence Storage.
		HallCatolog = HallBook.GetInstance(); //Singleton Class.. Also loads from Persistence Storage.
		CustomerCatolog = CustomerBook.GetInstance();//Singleton Class.. Also loads from Persistence Storage.
		BookingCatolog = BookingBook.GetInstance();//Singleton Class.. Also loads from Persistence Storage.
		ThreadLock=false;
	}
	
	/*
	 * Allows User Interface to Inquire about Viewing All Events in system.
	 */
	public ArrayList<Event> ViewAllEvents()
	{
		return EventCatolog.getEvents();
	}
	
	/*
	 * Book a Person or group to an Event
	 */
	public void Book (String LastName, String FirstName, String PhoneNumber , int TotalPeople,int EventID) throws Exception
	{
		boolean EventStatus = GetEventStatus(EventID);

		if(EventStatus) // registration is full
		{
			throw new Exception("Event is Fully-Booked.");
		}
		
		else
		{
			//check if person is in the System, else create it.
			CustomerCatolog.getCustomers();
			
			for(Customer C : CustomerCatolog.getCustomers())
			{
				if(C.getLastName() == LastName && C.getFirstName() == FirstName)
				{
					Event temp2 = EventCatolog.Find(EventID);		
					BookingCatolog.getBookings().add(new Booking(C,TotalPeople,temp2));
					return;
				}
			}
			Customer temp = new Customer(LastName, FirstName, PhoneNumber);
			Event temp2 = EventCatolog.Find(EventID);
			CustomerCatolog.getCustomers().add(temp);		
			BookingCatolog.getBookings().add(new Booking(temp,TotalPeople,temp2));
			return;
		}
	}
	
	/*
	 * Checks if Given Event is Full or not. TRUE = FULL    FALSE = NOT FULL
	 */
	public boolean GetEventStatus(int EventID) throws Exception
	{
		Event temp = EventCatolog.Find(EventID);
		
		if(temp==null)
		{
			throw new Exception("Event Does not Exist");
		}
		
		int CapacityOfHall = temp.getLocation().getSeatCapacity();
		
		int TotalBookingsInEvent = BookingCatolog.GetTotalBookingsPerEventName(EventID);
		
		if(TotalBookingsInEvent >=CapacityOfHall)
		{
			return true;
		}
		
		else
		{
			return false;
		}
	}
	
	/*
	 * 
	 */
	public ArrayList<Booking> ViewAllBookingsForGivenEvent(int EventID)
	{
		ArrayList<Booking> tempStorage = new ArrayList<Booking>(3);
		
		for(Booking B : BookingCatolog.getBookings())
		{
			if(B.getEventRegistered().getID() == EventID)
			{
				tempStorage.add(B);
			}
		}
		return tempStorage;
	}
	
}//end of class
